package anylise;

import configMeta.ConfigFolderListMeta;
import output.OutputResConfig;
import output.OutputResFileNode;
import output.OutputResQueueNode;
import utils.FileUtils;

import java.io.File;

/**
 * Created by eran on 14-9-7.
 */
public class ScanFolderAnalyzerBasic
{

    /**
     * 设置默认不被扫描的文件
     */
    private static String[] EXCLUDE_FILE_NAME_ARRAY =
            {".DS_Store"};

    protected OutputResConfig mOutPutConfig;

    protected Boolean mIsReleaseModel;

    public final void setOutPutConfig(OutputResConfig _outPutConfig)
    {
        mOutPutConfig = _outPutConfig;
    }

    public final void setIsReleaseModel(Boolean _isReleaseModel)
    {
        mIsReleaseModel = _isReleaseModel;
    }

    public void analysisFolder(ConfigFolderListMeta _folderMeta)
    {

    }

    public final boolean isCanAnyliseFolder(String _folderType)
    {
        for (String interestedType : getInterestedFolderTypeArray())
        {
            if (_folderType.equalsIgnoreCase(interestedType))
            {
                return true;
            }
        }
        return false;
    }

    protected String[] getInterestedFolderTypeArray()
    {
        return null;
    }

    protected final void tryToPackCurrentFile(File file, String queueName, String _releasePath)
    {
        if (!isExcludeFile(file.getName()))
        {
            OutputResQueueNode queueNode = mOutPutConfig.getOrCreateQueueNodeByName(queueName);

            //去除后缀的文件名
            String fileName = FileUtils.getFileNameNoEx(file.getName());
            //后缀名成
            String ex = FileUtils.getExtensionName(file.getPath());

            //查找当前Queue下面是否有对应的加载Node
            //如果没有则创建新的加载Node放置在当前Queue下面
            //否则查看当前扩展名字是否为Atlas,如果为Atlas则用Atlas替换为当前的加载路径
            OutputResFileNode fileNode = getResFileNodeByName(fileName, queueNode);
            if (fileNode == null)
            {
                fileNode = new OutputResFileNode();
                fileNode.fileName = fileName;
                if (mIsReleaseModel)
                {
                    fileNode.filePath = "app:/" + _releasePath + "/" + file.getName();
                }
                else
                {
                    //使用"\\\\"才可以匹配到"\\" 具体看文章http://sundful.iteye.com/blog/224093
                    fileNode.filePath = "File:///" + file.getAbsolutePath().replaceAll("\\\\", "/");
                }
                fileNode.fileType = ex;
                queueNode.fileList.add(fileNode);
            }
            else if (ex.equals("atlas"))
            {
                //如果为Atlas 则更新当前的文件
                if (mIsReleaseModel)
                {
                    fileNode.filePath = "app:/" + _releasePath + "/" + file.getName();
                }
                else
                {
                    //使用"\\\\"才可以匹配到"\\" 具体看文章http://sundful.iteye.com/blog/224093
                    fileNode.filePath = "File:///" + file.getAbsolutePath().replaceAll("\\\\", "/");
                }
                fileNode.fileType = ex;
            }
        }
    }

    private OutputResFileNode getResFileNodeByName(String _fileName, OutputResQueueNode queue)
    {
        for (OutputResFileNode fileNode : queue.fileList)
        {
            if (fileNode.fileName.equals(_fileName))
            {
                return fileNode;
            }
        }
        return null;
    }

    private boolean isExcludeFile(String _fileName)
    {
        for (String excludeFileName : EXCLUDE_FILE_NAME_ARRAY)
        {
            if (excludeFileName.equals(_fileName))
            {
                return true;
            }
        }
        return false;
    }

}
